package com.sxz.sbs.module.web;

import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.sxz.sbs.R;


/**
 *WebView的配置
 */

public class WebViewConfig {
    private AppCompatActivity activity;
    private WebView webView;
    private ProgressBar progressBar;
    /**
     * 构造方法
     * @param activity
     * @param webView
     * @param progressBar
     */
    public WebViewConfig(AppCompatActivity activity, WebView webView, ProgressBar progressBar) {
        this.activity = activity;
        this.webView = webView;
        this.progressBar = progressBar;
        //初始化配置
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        //关闭缓存，解决回退异常的问题
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        setWebClient((BaseWebViewActivity) this.activity);
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        settings.setUseWideViewPort(true);
        setChromeClient(this.progressBar);
    }

    /**
     * 加载进度条
     * @param progressBar
     */
    private void setChromeClient(final ProgressBar progressBar) {
        this.webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (newProgress >= 100) {
                    progressBar.setVisibility(View.GONE);
                } else {
                    if (progressBar.getVisibility() != View.VISIBLE) {
                        progressBar.setVisibility(View.VISIBLE);
                    }
                    progressBar.setProgress(newProgress);
                }
            }
        });
    }

    /**
     * 使用本地加载
     * @param activity
     */
    private void setWebClient(final BaseWebViewActivity activity) {
        webView.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                //解决页面中有代码控制回退导致回退紊乱的问题，返回false代表将操作交给webView来处理回退问题
                return false;
            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);

            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
                if (errorCode == (WebViewClient.ERROR_CONNECT
                        | WebViewClient.ERROR_IO
                        | WebViewClient.ERROR_TIMEOUT
                        | WebViewClient.ERROR_BAD_URL)) {
                    activity.setToolbarTitle(activity.getString(R.string.WEB_VIEW_FAIL_TITLE));
                }
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                String title = view.getTitle();
                if (title != null) {
                    if (title.length() > 6) {
                        title = title.substring(0, 5) + "...";
                    } else if (TextUtils.equals("找不到网页", title)) {
                        title = activity.getString(R.string.WEB_VIEW_FAIL_TITLE);
                    }
                } else {
                    title = "";
                }
                activity.setToolbarTitle(title);
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                activity.setToolbarTitle("加载中...");
            }
        });
    }


    public void loadUrl(String url) {
        webView.loadUrl(url);
    }

    public void loadUrl(String url, byte[] postData) {
        webView.postUrl(url, postData);
    }
    public void onResume() {
        webView.onResume();
    }

    public void onPause() {
        webView.onPause();
    }

    public void onDestroy() {
        webView.destroy();
    }

    public void onBackPress() {
        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            this.activity.finish();
        }
    }
}
